/**
 * 
 * @authors youzhao.zhou@pandauu.com
 * @date    2016-09-08 15:12:44
 * @version $Id$
 * 
 * 车票兑换页
 * 
 */

;(function(root,factory){

  var dependent = {
    zepto : typeof exports !== 'undefined' ? require('./zepto.js')&&root.$ : root.$,
    underscore : typeof exports !== 'undefined' ? require('../../public_src/js/underscore/underscore.js') : root._,
    Project : typeof exports !== 'undefined' ? require('../../public_src/js/1_self/1_project.js') : root.Project,
    conf : typeof exports !== 'undefined' ? require('../../public_src/js/1_self/2_config.js') : root.conf,
    log : typeof exports !== 'undefined' ? require('../../public_src/js/1_self/3_log.js') : root.p_log,
    path: {
      zepto : '../../p_src/js/convert/zepto.js',
      underscore : '../../public_src/js/underscore/underscore.min',
      Project : '../../public_src/js/1_self/1_project',
      conf : '../../public_src/js/1_self/2_config',
      log : '../../public_src/js/1_self/3_log',
    }
  };
  
  if(typeof exports === 'object' && typeof module === 'object'){

    module.exports = factory(exports,
      dependent.zepto, 
      dependent.underscore,
      dependent.Project, 
      dependent.conf,
      dependent.log
    );

  } else if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.

    define(['exports', 
      dependent.path.zepto, 
      dependent.path.underscore,
      dependent.path.Project, 
      dependent.path.conf,
      dependent.path.log
      ], factory);
  } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
    // CommonJS
    factory(exports, 
      dependent.zepto, 
      dependent.underscore,
      dependent.Project, 
      dependent.conf,
      dependent.log
    );
  } else {
    // Browser globals
    if (!(dependent.Project||dependent.conf||dependent.log)) {
      throw('请加载相应的文件！');
    }
    factory({},
      dependent.zepto, 
      dependent.underscore,
      dependent.Project, 
      dependent.conf,
      dependent.log
    );
  }

})((typeof window !== 'undefined' ? window : this) ,function(exports,$ ,_ , Project, conf, log, undefined){
  'use strict';

  setTimeout(function(){

    if (typeof $ === 'undefined') {
      $ = window.$;
    }

    if (typeof _ === 'undefined') {
      _ = window._;
    }

    /*
      调试开关
    */
    log._logToggle(true);

    var urlConf = conf._conf;
    /*
      项目工具类对象
    */
    var project = new Project();

    var params = project.getQueryStringObj();
    /*
      设置导航头部
    */
    project.setHTML('.middle', '备注');

    $('.jsRemark').attr({
      'data-id': params.thread_id
    });
    
    $.ajax({
      url: urlConf.apiRoot + urlConf.apiPre.one + urlConf.api.tagsRemarks,
      type: 'GET',
      dataType: 'json',
      data: {
        thread_id : params.thread_id
      }
    })
    .done(function(data) {
      if (data.code === 0) {
        renderLabel(_,data.tags);
        renderRemark(_,data.remarks);
        imgDefer();
      }else if(data.code === 100){
          window.location.href = urlConf.root + urlConf.static + 'login.php';
      }
      
    })
    .fail(function() {
      log._log("error");
    })
    .always(function() {
      log._log("complete");
    });

    
    require('./writing.js');
    require('./enlarge.js');


    /*
      渲染标签列表
    */
    function renderLabel(_, data){
      var tpl = $('#labelTpl');

      $('.jsLabelLists').html(_.template(tpl.html())({list: data}));

    }
    /*
      渲染备注列表
    */
    function renderRemark(_, data){
      var tpl = $('#remarkTpl');

      $('.jsRemarkUl').html(_.template(tpl.html())({list: data}));

    }

    /*
      图片延时加载
    */

    function imgDefer(){
      var isDeferr = $('[data-deferr]').data('deferr');
      var doms = $('[data-deferr]');
      
      doms.each(function(index, el) {
        if (isDeferr != 1) {
          return false;
        }
        var self = this;
        var imgUrl = urlConf.root + $(self).data('url');

        var img = new Image();

          img.onload = function(){
            $(self).attr({
              src : imgUrl,
              'data-img' : imgUrl
            });
          };

          img.src = imgUrl;
      });

    }

    window.addEventListener('unload',function(){
      log = null;
    },false);

  },0);

});
